package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ICrmMemberRelationDAO;
import org.jsola.hr.dto.CrmMemberRelationAddDTO;
import org.jsola.hr.dto.CrmMemberRelationUpdateDTO;
import org.jsola.hr.entity.CrmMemberRelationDO;
import org.jsola.hr.query.CrmMemberRelationQuery;
import org.jsola.hr.service.ICrmMemberRelationService;
import org.jsola.hr.vo.CrmMemberRelationListVO;
import org.jsola.hr.vo.CrmMemberRelationVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 成员关系表
 *
 * @author zhr
 */
@Service("hrCrmMemberRelationServiceImpl")
public class CrmMemberRelationServiceImpl implements ICrmMemberRelationService {

    @Autowired
    private ICrmMemberRelationDAO crmMemberRelationDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public CrmMemberRelationVO save(CrmMemberRelationAddDTO crmMemberRelationAddDTO, TokenUser tokenUser) {
        // 转DO
        CrmMemberRelationDO crmMemberRelationDO = crmMemberRelationAddDTO.to(CrmMemberRelationDO.class);
        // 保存
        crmMemberRelationDO = save(crmMemberRelationDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return crmMemberRelationDO.to(CrmMemberRelationVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(CrmMemberRelationUpdateDTO crmMemberRelationUpdateDTO, TokenUser tokenUser) {
        //转DO
        CrmMemberRelationDO crmMemberRelationDO = crmMemberRelationUpdateDTO.to(CrmMemberRelationDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(crmMemberRelationDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... crmMemberRelationIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) crmMemberRelationIds);
    }


    @Override
    public CrmMemberRelationVO selectById(String crmMemberRelationId, String siteId) {
        CrmMemberRelationDO crmMemberRelationDO = selectDOById(crmMemberRelationId, siteId);
        if (crmMemberRelationDO == null) {
            return null;
        }
        return crmMemberRelationDO.to(CrmMemberRelationVO.class);
    }

    @Override
    public List<CrmMemberRelationListVO> select(CrmMemberRelationQuery crmMemberRelationQuery, String siteId) {
        List<CrmMemberRelationDO> crmMemberRelationDOList = selectDO(crmMemberRelationQuery, siteId);
        if(CollectionUtils.isEmpty(crmMemberRelationDOList)) {
            return crmMemberRelationDOList == null ? null : new ArrayList<>();
        }
        return crmMemberRelationDOList.stream()
                .map(crmMemberRelationDO -> crmMemberRelationDO.to(CrmMemberRelationListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(CrmMemberRelationQuery crmMemberRelationQuery, String siteId) {
        Example example = buildExample(crmMemberRelationQuery,siteId);
        return crmMemberRelationDAO.selectCountByExample(example);
    }

    @Override
    public Page<CrmMemberRelationListVO> selectPage(CrmMemberRelationQuery crmMemberRelationQuery, String siteId) {
        Example example = buildExample(crmMemberRelationQuery,siteId);
        Page<CrmMemberRelationDO> page = crmMemberRelationDAO.selectPageByExample(example,
                crmMemberRelationQuery.getPageNo(),
                crmMemberRelationQuery.getPageSize());

        return page.to(CrmMemberRelationListVO.class);
    }

    @Override
    public CrmMemberRelationDO selectDOById(String crmMemberRelationId, String siteId) {
        return listById(crmMemberRelationId, siteId);
    }

    @Override
    public List<CrmMemberRelationDO> selectDO(CrmMemberRelationQuery crmMemberRelationQuery, String siteId) {
        Example example = buildExample(crmMemberRelationQuery,siteId);
        return crmMemberRelationDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param crmMemberRelationQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(CrmMemberRelationQuery crmMemberRelationQuery, String siteId) {
        Example example = new Example(CrmMemberRelationDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        if(!StringUtils.isEmpty(crmMemberRelationQuery.getSuperiorMemberId())){
            example.and().andEqualTo("superiorMemberId",crmMemberRelationQuery.getSuperiorMemberId());
        }
        ExampleKit.setExampleOrder(example,crmMemberRelationQuery.getOrders());
        return example;
    }
}




